﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Caching;

namespace Common
{
    /// <summary>
    /// WEB缓存辅助类
    /// </summary>
    public class CacheUtil
    {
        #region  将指定项添加到缓存

        /// <summary>
        /// 将指定项添加到缓存，如果缓存存在则添加失败
        /// </summary>
        /// <param name="key">用于引用该项的缓存键</param>
        /// <param name="value">要添加到缓存的项</param>
        public static void Set<T>(string key, T value)
        {
            Set(key, value, 3600 * 12);
        }

        /// <summary>
        /// 将指定项添加到缓存，如果缓存存在则添加失败
        /// </summary>
        /// <param name="key">用于引用该项的缓存键</param>
        /// <param name="value">要添加到缓存的项</param>
        /// <param name="duration">缓存过期时间,以秒为单位</param>
        public static void Set<T>(string key, T value, double duration)
        {
            System.Web.HttpRuntime.Cache.Add(key, value, null, DateTime.Now.AddSeconds(duration), TimeSpan.Zero, CacheItemPriority.Default, null);
        }

        /// <summary>
        /// 将指定项添加到缓存，如果缓存存在则添加失败
        /// </summary>
        /// <param name="key">用于引用该项的缓存键</param>
        /// <param name="value">要添加到缓存的项</param>
        /// <param name="duration">缓存过期时间,以秒为单位</param>
        /// <param name="cacheDependency">缓存依赖关系对象</param>
        public static void Set<T>(string key, T value, double duration, CacheDependency cacheDependency)
        {
            System.Web.HttpRuntime.Cache.Add(key, value, cacheDependency, DateTime.Now.AddSeconds(duration), TimeSpan.Zero, CacheItemPriority.Default, null);
        }


        /// <summary>
        /// 将指定项添加到缓存
        /// </summary>
        /// <param name="key">用于引用该项的缓存键</param>
        /// <param name="value">要添加到缓存的项</param>
        /// <param name="filename">缓存对象所依赖的文件或目录的路径</param>
        /// SDF 2011.06.22  添加
        public static void Set<T>(string key, T value, string filename)
        {
            CacheDependency dependency = new CacheDependency(filename);
            System.Web.HttpRuntime.Cache.Add(key, value, dependency, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 30, 0), CacheItemPriority.Default, null);
        }
        #endregion

        #region 将指定项插入到缓存
        /// <summary>
        /// 将指定项插入到缓存，如果缓存存在则则会替换
        /// </summary>
        /// <param name="key">用于引用该项的缓存键</param>
        /// <param name="value">要添加到缓存的项</param>
        /// <param name="duration">缓存过期时间,以秒为单位</param>
        public static void Insert<T>(string key, T value, double duration)
        {
            System.Web.HttpRuntime.Cache.Insert(key, value, null, DateTime.Now.AddSeconds(duration), TimeSpan.Zero, CacheItemPriority.Default, null);
        }

        /// <summary>
        /// 将指定项插入到缓存，如果缓存存在则则会替换
        /// </summary>
        /// <param name="key">用于引用该项的缓存键</param>
        /// <param name="value">要添加到缓存的项</param>
        /// <param name="duration">缓存过期时间,以秒为单位</param>
        /// <param name="cacheDependency">缓存依赖关系对象</param>
        public static void Insert<T>(string key, T value, double duration, CacheDependency cacheDependency)
        {
            System.Web.HttpRuntime.Cache.Insert(key, value, cacheDependency, DateTime.Now.AddSeconds(duration), TimeSpan.Zero, CacheItemPriority.Default, null);
        }
        #endregion

        #region 将指定项从缓存中移除
        /// <summary>
        /// 将指定项从缓存中移除
        /// </summary>
        /// <param name="key">用于引用该项的缓存键</param>
        public static void Remove(string key)
        {
            System.Web.HttpRuntime.Cache.Remove(key);
        }
        #endregion

        #region 判断缓存中存在指定项
        /// <summary>
        /// 判断缓存中存在指定项
        /// </summary>
        /// <param name="key">用于引用该项的缓存键</param>
        /// <returns>是否存在</returns>
        public static bool Exist(string key)
        {
            return System.Web.HttpRuntime.Cache[key] != null;
        }
        #endregion

        #region 从断缓存中检索指定项
        /// <summary>
        /// 从断缓存中检索指定项
        /// </summary>
        /// <param name="key">用于引用该项的缓存键</param>
        /// <returns>缓存的项</returns>
        public static T Get<T>(string key)
        {
            return (T)System.Web.HttpRuntime.Cache[key];
        }
        #endregion
    }
}
